package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.dto.AnimalMarkDto;
import com.jiantong.work.model.AnimalMark;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 6/7/17.
 *
 */
@Repository
public class AnimalMarkDao extends MyBatisDao<AnimalMark> {

    public Paging<AnimalMark> pagingAnimal(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = this.sqlSession.selectOne(this.sqlId("countAnimal"), criteria);
        if(total <= 0L) {
            return new Paging(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List datas = this.sqlSession.selectList(this.sqlId("pagingAnimal"), criteria);
            return new Paging(total, datas);
        }
    }

    public Paging<AnimalMark> pagingSelectAnimalMak(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = this.sqlSession.selectOne(this.sqlId("countSelectAnimalMak"), criteria);
        if(total <= 0L) {
            return new Paging(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List datas = this.sqlSession.selectList(this.sqlId("pagingSelectAnimalMak"), criteria);
            return new Paging(total, datas);
        }
    }

    public Paging<AnimalMarkDto> pagingByBreedFactoryName(Integer offset, Integer limit,Map<String , Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = this.sqlSession.selectOne(this.sqlId("countByBreedFactoryName"),criteria);
        if(total <= 0L) {
            return new Paging(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            //能不能查询出来List<AnimalMarkDto>?
            List datas = this.sqlSession.selectList(this.sqlId("pagingByBreedFactoryName"),criteria);//

            return new Paging(total, datas);
        }
    }

    public Boolean updateByNumber(AnimalMark animalMark) {
        return this.sqlSession.update(this.sqlId("updateByNumber"), animalMark) == 1;
    }


    /**
     * 根据残缺耳标号模糊查询出一个耳标号list
     * @param criteria
     * @return
     */
    public Paging<AnimalMark> PagingFindByMark(Integer offset, Integer limit, Map<String, Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = this.sqlSession.selectOne(this.sqlId("countFindByMark"), criteria);
        if(total <= 0L) {
            return new Paging(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List datas = this.sqlSession.selectList(this.sqlId("pagingFindByMark"), criteria);
            return new Paging(total, datas);
        }


//        return this.sqlSession.selectList(this.sqlId("findByMark"),map);
    }

    /**
     * 查询排列靠前的十个状态为入库的耳标
     * @param map
     * @return
     */
    public List<AnimalMark> findByMarkIsNull(Map<String, Object> map){
        return this.sqlSession.selectList(this.sqlId("findByMarkIsNull"),map);
    }

    /**
     * map 中 status  1 ;  ids的list
     * @param map
     * @return
     */
    public Boolean updates(Map map){
       return this.sqlSession.update(this.sqlId("updates"), map)>=1;
    }

    /**
     * map 中 status  1 ;  ids的list
     * @param map
     * @return
     */
    public Boolean updateMarks(Map map){
       return this.sqlSession.update(this.sqlId("updateMarks"), map)>=1;
    }


    /**
     * 根据耳标号查询耳标号的具体信息
     * @param criteria 耳标号
     * @return AnimalMark
     */
    public AnimalMark findByMarkNumber(Map<String , Object> criteria){
        return getSqlSession().selectOne(sqlId("findByMarkNumber"),criteria);
    }

    public AnimalMark findByFullMarkNumber(String markNumber){
        return getSqlSession().selectOne(sqlId("findByFullMarkNumber"),markNumber);
    }
 }
